package br.com.sgi.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;

import org.springframework.stereotype.Service;

import br.com.sgi.exception.SgiException;
import br.com.sgi.service.RelatorioService;
import br.com.sgi.util.RelatorioUtil;

@Service("relatorioServcie")
public class RelatorioServiceImpl implements RelatorioService {

	@Override
	public void gerarRelatorioPdf(List<?> listaObjetos, Map<String, Object> parametros,
			String nomeArquivo, String nomeRelatorio) throws SgiException {

		try {
			HttpServletResponse response = RelatorioUtil.preparaPaginaPdf(RelatorioUtil
					.getContext(), nomeArquivo);
			JasperExportManager.exportReportToPdfStream(RelatorioUtil.construirRelatorio(
					nomeRelatorio, parametros, listaObjetos), response.getOutputStream());
			RelatorioUtil.getContext().responseComplete();
		} catch (JRException e) {
			e.printStackTrace();
			throw new SgiException("erro.MSG_08");
		} catch (IOException e) {
			e.printStackTrace();
			throw new SgiException("info.MSG_01");
		}

	}

}
